package com.nrgedge.magnolia.form.processors;

import info.magnolia.cms.core.Content;
import info.magnolia.context.MgnlContext;
import info.magnolia.module.form.processors.AbstractFormProcessor;
import info.magnolia.module.form.processors.FormProcessorFailedException;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.nrgedge.magnolia.CaptchaServiceSingleton;

public class JCaptchaFormProcessor extends AbstractFormProcessor {
		
	@Override
	protected void internalProcess(Content content,	Map<String, Object> parameters) throws FormProcessorFailedException {
		final HttpServletRequest request = MgnlContext.getWebContext().getRequest();
		final String captchaId = request.getSession().getId();
		boolean isResponseCorrect = false;
        //retrieve the response
        String response = request.getParameter("j_captcha_response");

        isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,
                response);
        
        if (!isResponseCorrect) {
        	throw new FormProcessorFailedException("JCaptchaFormProcessor.errorMessage");
        }
	}

}
